/*
 * ActionMgr.h
 *
 *  Created on: 2012-9-4
 *      Author: yangentao@gmail.com
 */

#ifndef ACTIONMGR_H_
#define ACTIONMGR_H_


#include "../dep.h"

class ActionManager;

class ITextReplacable {
	friend class ActionManager;
public:
	virtual ~ITextReplacable();
protected:
	//return old text
	virtual String textReplace(int from, int len_, const String& newText) = 0;
};

class Action {
	friend class ActionManager;
protected:
	int from;
	int length;
	String oldText;
	String newText;
	long groupId;

public:
	Action(int from_, int len_, String newText_);

	void dump();

};

class ActionManager {
private:
	Stack<Action> undos;
	Stack<Action> redos;

	long groupId;
	ITextReplacable* textReplacable;
public:
	ActionManager(ITextReplacable* textReplacable_ = 0) ;
	void setTextReplacable(ITextReplacable* textReplacable_);
private:
	void performAction(Action& action) ;
	void performUndo(Action& action);
	vector<Action> popGroup(Stack<Action>& s) ;
public:
	void beginGroup();
	void endGroup();
	void doAction(Action action);
	void undo() ;
	void redo();
	bool canUndo();
	bool canRedo() ;
	void clear();


};


#endif /* ACTIONMGR_H_ */
